a<?php
class System_PagesModel extends CI_Model {
	function __construct() {
		parent::__construct ();
		$this->load->database ();
	}
	
	function getPages($id = FALSE) {
		if ($id === FALSE) {
			$query = $this->db->get ( 'system_pages' );
			if ($query->num_rows() <= 0){
				return FALSE;
			}
			return $query->result_array ();
		} else {
			$query = $this->db->get_where ( 'system_pages', array ('id' => $id ) );
			if ($query->num_rows() <= 0){
				return FALSE;
			}
			return $query->row_array ();
		}
	}
	
	function add($data){
		if ($data == NULL) return FALSE;
		$this->db->insert('system_pages', $data);
		return TRUE;
	}
	
	function update($id, $data) {
		foreach ( $data as &$d ) {
			$d = $this->db->escape_str ( $d );
		}
		if (isset ( $data ['page_title'] )) {
			$newdata ['page_title'] = $data ['page_title'];
		}
		if (isset ( $data ['content'] )) {
			$newdata ['content'] = $data ['content'];
		}
		if (isset ( $data ['position'] )) {
			$newdata ['position'] = $data ['position'];
		}
		if (isset ( $data ['status'] )) {
			$newdata ['status'] = $data ['status'];
		}
		
		$this->db->where ( 'id', $id );
		$query = $this->db->get ( 'system_pages' );
		
		if ($query->num_rows () == 0)
			return false;
		else {
			$this->db->where ( 'id', $id );
			if ($this->db->update ( 'system_pages', $newdata ))
				return true;
			else
				return false;
		}
	}
	
	function delete($id) {
		$this->db->where ( 'id', $id );
		$query = $this->db->delete ( 'system_pages' );
		if (! $query)
			return false;
		else
			return true;
	}
}